<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>管理员维护</title>
    <jsp:include page="/IncludeJS.jsp"></jsp:include>
</head>
<body onload="init();">
<script type="text/javascript">
    let imageType = ["jpg","png","bmp"]

    function check() {
        if ($("#managerAccount").val() == "") {
            alert("请输入管理员账户");
            $('#managerAccount').focus();
            return false;
        }

        if ($("#id").val() == "" && $("#managerPassword").val() == "") {
            alert("请输入登录密码");
            $('#managerPassword').focus();
            return false;
        }
        if ($("#id").val() == "" && $("#managerPassword").val() != $("#managerPassword1").val()) {
            alert("登录密码与确认密码不一致");
            $('#managerPassword1').focus();
            return false;
        }
        if ($("#managerName").val() == "") {
            alert("请输入管理员姓名");
            $('#managerName').focus();
            return false;
        }
        if ($("#id").val() == "") {
            $("#id").val(0);
        }
        let haveHead = '${manager.headimg}'
        if (haveHead != null){
            return true;
        }

        if ($("#file").val() == "") {
            alert("请选择头像");
            $('#file').focus();
            return false;
        }else {
            let type = $("#file").val().match(/^(.*)(\.)(.{1,8})$/)[3].toLowerCase();
            for (const i in imageType) {
                if (imageType[i] === type){
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    function init() {
        var key = $("#id").val();

    }

    function del() {
        var id = $("#id").val();
        if (id == null || id == "") {
            alert("没有可以删除的数据");
            return false;
        }
        if (!confirm('删除确定')) {
            return false;
        }
        var url = "${pageContext.request.contextPath }/managerAction!del.action?delsingle=2&ids=" + id;
        location.href = url;
    }

    function add() {
        bAdd = true;
        var url = "${pageContext.request.contextPath }/managerAction!init.action";
        //var title="新闻资讯新增";
        //addTab1(title, url);
        location.href = url;
    }

    function back() {
        location.href = "${pageContext.request.contextPath }/list";
    }

    if ("${msg}" != null && "${msg}".length > 0) {
        alert("${msg}");
    }

</script>
<table class="tablelist2">
    <tbody>
    <tr>
        <td class="tableselect2" style="border-bottom:none;">
            <div class="tools">
                <ul class="toolbar">
                    <li class="click"><a href="javascript:void(0);" onclick="back();"><i></i>返回</a></li>
                </ul>
            </div>
        </td>
    </tr>
    </tbody>
</table>
<form action="${pageContext.request.contextPath }/save" id="managerform" enctype="multipart/form-data" method="post" onsubmit="return check();">
    <input type="hidden" name="managerId" id="id" value="${manager.managerId}"/>
    <table cellpadding="0" cellspacing="0" width="1000" style="margin: 0 auto">
        <tr height="30">
            <td align="right" width="90">管理员账户</td>
            <td><input type="text" name="managerAccount" id="managerAccount" value="${manager.managerAccount}"
                       style="width:200px"/></td>
        </tr>
        <tr height="30">
            <td align="right">机构名称</td>
            <td>
                <select name="agentId" id="agentId" style="width:200px">
                    <c:forEach items="${agentList}" var="v">
                        <c:if test="${manager.agentId == v.agentId}">
                            <option value="${v.agentId}" selected>${v.agentName}</option>
                        </c:if>
                        <c:if test="${manager.agentId != v.agentId}">
                            <option value="${v.agentId}">${v.agentName}</option>
                        </c:if>
                    </c:forEach>
                </select>

                <%--                <input type="text" name="agentName" id="agentName" style="width:200px"--%>
                <%--                       onkeyup="changeAgent2(this.value);"/>--%>
            </td>

        </tr>
        <tr height="30">
            <td align="right">登录密码</td>
            <td><input type="password" name="managerPassword" id="managerPassword" style="width:200px"/></td>
        </tr>
        <tr height="30">
            <td align="right">确认密码</td>
            <td><input type="password" name="managerPassword1" id="managerPassword1" style="width:200px"/></td>
        </tr>
        <tr height="30">
            <td align="right">管理员类型</td>
            <td>
                <select name="managerType" id="managerType" style="width:200px">
                    <c:forEach items="${mtList}" var="v">
                        <c:if test="${manager.managerType==v.id}">
                            <option value="${v.id}" selected>${v.name}</option>
                        </c:if>
                        <c:if test="${manager.managerType != v.id}">
                            <option value="${v.id}">${v.name}</option>
                        </c:if>
                    </c:forEach>
                </select>
            </td>
        </tr>
        <tr height="30">
            <td align="right">管理员头像</td>
            <td><input type="file" name="file" id="file"
                       style="width:200px"/></td>
            <td rowspan="5" valign="top">
                <img src="${pageContext.request.contextPath }/${manager.headimg}" width="150"
                                              align="top"></td>
        </tr>
        <tr height="30">
            <td align="right">管理员姓名</td>
            <td><input type="text" name="managerName" id="managerName" value="${manager.managerName}"
                       style="width:200px"/></td>
        </tr>
        <tr height="30">
            <td align="right">性别</td>
            <td>
                <select name="sex" id="sex" style="width:200px">
                    <option value="0" <c:if test="${manager.sex==0}">selected</c:if>>男</option>
                    <option value="1" <c:if test="${manager.sex==1}">selected</c:if>>女</option>
                </select>
            </td>
        </tr>
        <tr height="30">
            <td align="right">状态</td>
            <td>
                <select name="invalid" id="invalid" style="width:200px">
                    <option value="0" <c:if test="${manager.invalid==0}">selected</c:if>>禁用</option>
                    <option value="1" <c:if test="${manager==null || manager.invalid==1}">selected</c:if>>启用</option>
                </select>
            </td>
        </tr>

        <tr height="30">
            <td align="right">联系方式</td>
            <td><input type="text" name="contact" id="contact" value="${manager.contact}" style="width:200px"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="  保 存  " name="isEmpty"></td>
        </tr>
    </table>
</form>

<script type="text/javascript">
    var orgjson = {"data": []};

    function initAgentJson() {
        $("#agentId option").each(function () {
            var item = {"value": this.value, "text": this.text};
            orgjson.data.push(item);
        });
    }

    initAgentJson();

    function changeAgent2(value) {
        $("#agentId").empty();
        $.each(orgjson.data, function (i, item) {
            if (item["text"].indexOf(value) >= 0) {
                $("#agentId").append("<option value='" + item["value"] + "'>" + item["text"] + "</option>");
            }
        });
    }
</script>
</body>
</html>